<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\ArticleCate;
use app\common\model\VideoCate;
use think\facade\Db;
use think\facade\View;
use think\Request;
use think\exception\ValidateException;

class AdminVideoCate extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
       
        //        父级分类
        $list = VideoCate::where(1)->order("ordid,id DESC")->select()->toArray();

        foreach ($list as $k => $v){
            //排除掉不需要纳入考核的项目
            if(isset($v["status"]))unset($v["status"]);
            if(isset($v["pid"]))unset($v["pid"]);
            if(isset($v["oid"]))unset($v["oid"]);

            //分母
            $fenmu = count($v);
            $fenzi = 0;
            foreach ($v as $m){
                if($m){
                    $fenzi ++;
                }
            }
            $percent = round($fenzi/$fenmu,2);
            $list[$k]["percent"] = $percent;
        }

        //无限极分类后的 分类
        $handle_list = unlimitforlevel($list);

        View::assign("handle_list",$handle_list);

        View::assign("list",$list);

    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        View::assign("title","文章发布");
        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $mod = new \app\common\model\VideoCate();

        $d = $request->post();
        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);

        try {
            validate(\app\common\validate\ArticleCate::class)->check($d);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return  json(["code"=>4000,"msg"=>$e->getError()]);
        }

        if(!$d["name"]){
            return  json(["code"=>4000,"msg"=>"name不能为空"]);
        }

        if(isset($d["id"])){
            $res = Db::name("article_cate")->strict(false)->where("id",$d["id"])->update($d);
        }else{
            $res = $mod->save($d);
        }

        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

  

    

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = ArticleCate::find($id);
        View::assign("res",$res);
        return view('create');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = VideoCate::destroy($id);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

}
